package com.sen.service.impl;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.unit.DataUnit;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sen.constant.StockConstant;
import com.sen.dao.StockDao;
import com.sen.entity.DruginfoEntity;
import com.sen.entity.StockEntity;
import com.sen.entity.StockheadEntity;
import com.sen.entity.vo.poOrderVo;
import com.sen.service.DruginfoService;
import com.sen.service.StockService;
import com.sen.service.StockheadService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

@Service("stockService")
public class StockServiceImpl extends ServiceImpl<StockDao, StockEntity> implements StockService {
    //表头
    @Autowired
    private StockheadService stockheadService;
    @Autowired
    private DruginfoService druginfoService;

    //添加订单
    @Override
    public void saveOrder(List<poOrderVo> poOrderVos, Double money,Integer seid) {
        //查看价格是否和药品信息相同
        List<DruginfoEntity> drugList = druginfoService.list();
        //遍历前台传递的数据
        for (poOrderVo poOrderVo : poOrderVos) {
            //获取所有药品id
            for (DruginfoEntity druginfoEntity : drugList) {
                //如果药品id相同
                if (druginfoEntity.getMid().equals(poOrderVo.getMid())){
                    //如果两者价格不相同则进行更改
                    if (!druginfoEntity.getPrice().equals(poOrderVo.getPrice())){
                        druginfoEntity.setPrice(poOrderVo.getPrice());
                        druginfoService.updateById(druginfoEntity);
                    }
                }
            }
        }

        //订单详细
        String hno=null;
        //遍历前台数据
        for (poOrderVo poOrderVo : poOrderVos) {
            //获取订单编号
            hno = poOrderVo.getHno();
            StockEntity stockEntity = new StockEntity();
            BeanUtils.copyProperties(poOrderVo,stockEntity);
            stockEntity.setDid(poOrderVo.getMid());
            System.out.println(stockEntity);
            //插入详情表
            this.baseMapper.insert(stockEntity);
        }

//        poOrderVos.stream().map(poOrderVo ->{
//            return poOrderVo;
//        }).



        //订单头
        StockheadEntity stockheadEntity = new StockheadEntity();
        stockheadEntity.setHno(hno);
        stockheadEntity.setEid(0);
        stockheadEntity.setMoney(money);
        stockheadEntity.setState(StockConstant.StateEnum.STATE_UNDER_REVIEW.getState());
        stockheadEntity.setSeid(seid);
        System.out.println(stockheadEntity);
        stockheadService.save(stockheadEntity);

    }

}
